//
//  RadioViewController.m
//  A手工客
//
//  Created by 萌萌的小坤坤 on 16/6/13.
//  Copyright © 2016年 冷坤. All rights reserved.
//

#import "RadioViewController.h"
#import "VideoTabView.h"
#import "VideoModel.h"
#import "AFNetworking.h"
#define RADIONET @"http://m.shougongke.com/index.php?c=Handclass&a=videoList&cate=0&page=1&price=0&sort=1&vid=18"
#define WIDTH self.view.frame.size.width

#define HEIGHT self.view.frame.size.height

#define COLORS [UIColor colorWithRed:(arc4random() % 256 / 255.0) green:(arc4random() % 256 / 255.0) blue:(arc4random() % 256 / 255.0) alpha:1.0];

@interface RadioViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic ,retain) UITableView *tabView;
@property (nonatomic ,retain) NSMutableArray *arrRadio;

@end

@implementation RadioViewController
- (NSMutableArray *)arrRadio{
    if (!_arrRadio) {
        self.arrRadio = [NSMutableArray array];
    }
    return _arrRadio;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getRadioUrl];
    [self creatTabView];

}

- (void)creatTabView{
    self.tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
    self.tabView.delegate = self;
    self.tabView.dataSource = self;
    
    [self.view addSubview:self.tabView];
    [_tabView release];    
}

# pragma mark - 网络请求
- (void)getRadioUrl{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil];
    
    [manager GET:RADIONET parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSMutableArray *arr = responseObject[@"data"];
        
        
//        NSDictionary *dic = responseObject[@"data"];
        
        for (NSDictionary *data in arr) {
            VideoModel *model = [VideoModel videoModelWithDic:data];
            
            [self.arrRadio addObject:model];
            
        }
        
        [self.tabView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
    }];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 667 / 3.5 + 20;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrRadio.count;
}

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    return self.arrRadio.count;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    
    VideoTabView *videoTab = [tableView dequeueReusableCellWithIdentifier:ID];
    if (videoTab == nil) {
        videoTab = [[VideoTabView alloc]initWithStyle:1 reuseIdentifier:ID];
//        videoTab.backgroundColor = COLORS;
        
        
    }
    VideoModel *models = self.arrRadio[indexPath.row];
    videoTab.model = models;

    return videoTab;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
